/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[TestNumberFormat.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package api.text;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

/**
 * java.text.NumberFormat测试
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class TestNumberFormat {

	/**
	 * 格式化数字
	 */
	public static void testFormatNumber() {
		NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.CHINA);
		System.out.println("Chinese number: " + numberFormat.format(new Double("2015.1118")));

		numberFormat = NumberFormat.getNumberInstance(Locale.FRANCE);
		System.out.println("France number: " + numberFormat.format(new Double("2015.1118")));
		System.out.println();
	}

	/**
	 * 格式化货币
	 */
	public static void testFormatCurrency() {
		NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.CHINA);
		System.out.println("Chinese currency: " + numberFormat.format(new Double("2015.1118")));

		numberFormat = NumberFormat.getCurrencyInstance(Locale.FRANCE);
		System.out.println("France currency: " + numberFormat.format(new Double("2015.1118")));
		System.out.println();
	}

	/**
	 * 格式化百分比
	 */
	public static void testFormatPercent() {
		NumberFormat numberFormat = NumberFormat.getPercentInstance(Locale.CHINA);
		System.out.println("Chinese precent: " + numberFormat.format(new Double("2015.1118")));

		numberFormat = NumberFormat.getPercentInstance(Locale.FRANCE);
		System.out.println("France percent: " + numberFormat.format(new Double("2015.1118")));
		System.out.println();
	}

	/**
	 * 将字符串转换为数字
	 */
	public static void parseStringToNumber() {
		String str = "2015.1118";
		try {
			Number number = NumberFormat.getInstance().parse(str);
			System.out.println("After format: " + number);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		System.out.println();
	}

	public static void main(String[] args) {
		testFormatNumber();
		testFormatCurrency();
		testFormatPercent();
		parseStringToNumber();
	}
}
